﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using DataAccessLibrary.DAL;
using DataAccessLibrary;
using entities;

namespace BusinessLogicLibrary
{
    public class EmployeeModule
    {
        private readonly EmployeeDAO _dao;
        private const string Fail = "FAIL: ";

        public EmployeeModule()
        {
            _dao = new EmployeeDAO();
        }

       
        public bool CheckEmployeeEmail(string email)
        {
            return _dao.CheckEmployeeEmail(email);
        }

        public string AddEmployee(string name, bool sex, DateTime birthday, string position, string phone, string email,
                                  string address, string city, string country, string jobtitle, byte[] image)
        {
            try
            {
                var utility = new Utility();

                var employee = new Employee
                                   {
                                       EmployeeName = name,
                                       EmployeeSex = sex,
                                       EmployeeBirthday = birthday,
                                       EmployeePosition = position,
                                       EmployeePhone = phone,
                                       EmployeeEmail = email,
                                       EmployeeAddress = address,
                                       EmployeeCity = city,
                                       EmployeeCountry = country,
                                       EmployeeJobTitle = jobtitle,
                                       EmployeeImage = image,
                                       EmployeeAccount = utility.CreateAccount(name),
                                       EmployeePassword = utility.CreateRandomPassword(6),
                                       EmployeeAvailable = true
                                   };
                return _dao.InsertEmployee(employee);
            }
            catch (Exception ex)
            {
                return Fail + ex.Message;
            }
        }


        public string ModifyEmployee(int employeeID, string name, bool sex, DateTime birthday, string position,
                                     string phone, string email, string address, string city, string country,
                                     string jobtitle, byte[] image)
        {
            var employee = new Employee
                               {
                                   EmployeeID = employeeID,
                                   EmployeeName = name,
                                   EmployeeSex = sex,
                                   EmployeeBirthday = birthday,
                                   EmployeePosition = position,
                                   EmployeePhone = phone,
                                   EmployeeEmail = email,
                                   EmployeeAddress = address,
                                   EmployeeCity = city,
                                   EmployeeCountry = country,
                                   EmployeeJobTitle = jobtitle,
                                   EmployeeImage = image,
                                   EmployeeAvailable = true
                               };
            return _dao.UpdateEmployee(employee);
        }

        public string DeactiveEmployee(int employeeID)
        {
            return _dao.DeactiveEmployee(employeeID);
        }

        public List<vEmployeeSubDetail> GetEmployee(bool all)
        {
            return _dao.SelectEmployee(all);
        }

        public Employee GetEmployeeById(int employeeID)
        {
            Employee employee = _dao.SelectEmployeeByID(employeeID);
            return employee;
        }

        public List<vEmployeeSubDetail> SearchEmployeeByName(string val)
        {
            return _dao.SearchEmployeeByName(val);
        }
    }
}